package com.taru.wxhsh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taru.wxhsh.common.JsonResult;
import com.taru.wxhsh.constant.JResponseConstants;
import com.taru.wxhsh.pojo.Product;

import com.taru.wxhsh.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 不能用RestController  有的页面是给后台用跳转
 */
@Controller
public class ProductController {
    Logger logger  = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    ProductService productService;

    /**
     * 测试代码
     * @return
     */
    @ResponseBody
    @RequestMapping("/product/list/test")
    public Object listTest(){
        JsonResult< List<Product>>   result=null;

        return  result;

    }


    /**
     * 商品查询
     * 2018年12月26日 新增缺货搜索
     * @return
     */
    @ResponseBody
    @RequestMapping("/product/list")
    public Object list(@RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam("categoryId") String categoryId,
                       @RequestParam("productName") String productName,
                       @RequestParam("isLack") String isLack

                    ){
        JsonResult< PageInfo<Product>>   result=null;
        try{
            PageHelper.startPage(pageNum,pageSize);
            List<Product> products =productService.findProductsList(categoryId,productName,isLack);
            PageInfo<Product> pageInfo =new PageInfo<>(products,pageSize);

            result =new JsonResult< PageInfo<Product>>(JResponseConstants.STATUS_OK,"查询商品列表成功",pageInfo);
        }catch (Exception ex){
            logger.error("查询商品列表异常:{}",ex.getMessage());
            result =new JsonResult< PageInfo<Product>>(JResponseConstants.STATUS_ERROR,"查询商品列表异常");
        }
        return  result;

    }

    /**
     * 商品查询根据ID
     * @return
     */
    @ResponseBody
    @RequestMapping("/product/find")
    public Object find(String  productId){
        JsonResult<Product>   result=null;
        try{

            Product product =productService.findProductById(productId);
            result =new JsonResult<>(JResponseConstants.STATUS_OK,"查询商品成功",product);
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("查询商品异常:{}",ex.getMessage());
            result =new JsonResult<>(JResponseConstants.STATUS_ERROR,"查询商品异常");
        }
        return  result;

    }


    @RequestMapping("/product/add")
    @ResponseBody
    public Object  add(Product  product){
        JsonResult<String> result = null;
        try {
            productService.addProduct(product);

            logger.info("商品参数：{}",product.getOtherDatas()==null);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"添加商品成功");
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("添加商品异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"添加商品异常");
        }
        return  result;

    }

    @RequestMapping("/product/drop")
    @ResponseBody
    public Object  drop(String  productId){
        JsonResult<String> result = null;
        try {
            productService.updateStatusById(productId);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"商品废弃成功");
        }catch (Exception ex){
            logger.error("商品废弃异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"商品废弃异常");
        }
        return  result;

    }

    /**
     * 根据商品ID  增加  排序数字
     * @param productId
     * @return
     */
    @RequestMapping("/product/upOrder")
    @ResponseBody
    public Object  upOrder(String  productId){
        JsonResult<String> result = null;
        try {
            productService.upProductOrderById(productId);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"商品修改成功");
        }catch (Exception ex){
            logger.error("商品修改异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"商品修改异常");
        }
        return  result;

    }

    /**
     * 根据商品ID  减少  排序数字
     * @param productId
     * @return
     */
    @RequestMapping("/product/downOrder")
    @ResponseBody
    public Object  downOrder(String  productId){
        JsonResult<String> result = null;
        try {
            productService.downProductOrderById(productId);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"商品修改成功");
        }catch (Exception ex){
            logger.error("商品修改异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"商品修改异常");
        }
        return  result;

    }

    @RequestMapping("/product/update")
    @ResponseBody
    public Object  update(Product  product){
        JsonResult<String> result = null;
        try {
            productService.updateProduct(product);

            logger.info("商品参数：{}",product.getOtherDatas()==null);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"修改商品成功");
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("修改商品异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"修改商品异常");
        }
        return  result;

    }

    /**
     * 缺货处理
     * @param id
     * @return
     */
    @RequestMapping("/product/lack")
    @ResponseBody
    public Object  updateLack(String id){
        JsonResult<String> result = null;
        try {
            productService.updateLackById(id);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"修改缺货成功");
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("修改缺货异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"修改缺货异常");
        }
        return  result;

    }

    /**
     * 重新上货，商品从缺货状态改变为可售卖状态
     * @param id
     * @return
     */
    @RequestMapping("/product/noLack")
    @ResponseBody
    public Object  updateNoLack(String id){
        JsonResult<String> result = null;
        try {
            productService.updateNoLackById(id);
            result = new JsonResult<>(JResponseConstants.STATUS_OK,"重新上货成功");
        }catch (Exception ex){
            ex.printStackTrace();
            logger.error("重新上货异常：{}",ex.getMessage());
            result = new JsonResult<>(JResponseConstants.STATUS_ERROR,"重新上货异常");
        }
        return  result;

    }



}
